/**
  ******************************************************************************
  *@file      rtx5.c
  *@author    hongyu zhang (zhy_5262@163.com)
  *@brief     
  ******************************************************************************
  *@date      2025-01-01
  *@version   0.1
  *@copyright Copyright (c) 2025 
  *
  *All rights reserved.
  *
  ******************************************************************************
  */

#include "cmsis_os2.h"
#include "stdio.h"
#include "main.h"

/* Definitions for ComTimer */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .stack_size = 256 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
osTimerId_t ComTimerHandle;
const osTimerAttr_t ComTimer_attributes = {
  .name = "ComTimer"
};
/* Definitions for LedTimer */
osTimerId_t LedTimerHandle;
const osTimerAttr_t LedTimer_attributes = {
  .name = "LedTimer"
};

void ComCallback(void* argument);
void LedCallback(void* argument);

void StartDefaultTask(void *argument);

/*----------------------------------------------------------------------------
 *      Initialize RTX Kernel
 *---------------------------------------------------------------------------*/
void RTX5_Init()
{
  ComTimerHandle = osTimerNew(ComCallback, osTimerPeriodic, NULL, &ComTimer_attributes);

  LedTimerHandle = osTimerNew(LedCallback, osTimerPeriodic, NULL, &LedTimer_attributes);

  osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
}

void StartDefaultTask(void *argument) {
  osStatus_t status;

  if (ComTimerHandle != NULL) {
    // start timer with delay 100ms
    status = osTimerStart(ComTimerHandle, 5000u);
    if (status != osOK) {
      return;
    }
  }

  if (ComTimerHandle != NULL) {
    // start timer with delay 100ms
    status = osTimerStart(LedTimerHandle, 2000u);
    if (status != osOK) {
      return;
    }
  }

  while(1) {
    osDelay(1);
  }

}

/* Callback01 function */
void ComCallback(void *argument)
{
  /* USER CODE BEGIN Callback01 */
    printf("Hello from callback 01\n");
  /* USER CODE END Callback01 */
}

/* Callback02 function */
void LedCallback(void *argument)
{
  /* USER CODE BEGIN Callback02 */
        HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
  /* USER CODE END Callback02 */
}
